package org.m2acsi.control;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.m2acsi.boundary.ConsultationEJB;
import org.m2acsi.boundary.MessageEJB;
import org.m2acsi.entities.Message;
import org.m2acsi.entities.Task;
import org.m2acsi.utils.Security;

@Named
@RequestScoped
@ManagedBean
public class MessageBean extends BaseBean implements Serializable {

    private String comment;
    private Message message = new Message();
    @Inject
    private MessageEJB messageEJB;
    @Inject
    private ConsultationEJB consultationEJB;

    public MessageBean() {
        super();
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void save() {
        String idTaskString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
        Task tmpTask = Security.taskSecurityCheck(idTaskString, getAuthenticatedUser(), taskEjb, this);
        if (tmpTask.isFinished()) {
            if (comment != null && comment.length() != 0) {
                message.setContent(comment);
                message.setTask(tmpTask);
                message.setUser(getAuthenticatedUser());
                messageEJB.save(message);
                List<Message> messages = tmpTask.getMessages();
                messages.add(message);
                tmpTask.setMessages(messages);
                taskEjb.update(tmpTask);

                redirectWithMessage("/faces/user/task.xhtml?id=" + idTaskString, "Your message has been posted.");
            } else {
                if (comment != null) {
                    FacesContext.getCurrentInstance().addMessage("message-add-form:message-text", new FacesMessage("Aren't you here to say something?"));
                }
            }
        } else {
            hackRedirect("try to edit a closed task.");
        }
    }

    public List<Message> getMessages() {
        Task tmpTask = Security.taskSecurityCheck(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"), getAuthenticatedUser(), taskEjb, this);
        Date lastVisit = consultationEJB.lastConsultationOnTaskDetails(tmpTask, getAuthenticatedUser());
        List<Message> messages = messageEJB.getMessages(tmpTask);
        for (Iterator<Message> it = messages.iterator(); it.hasNext();) {
            Message m = it.next();
            if (lastVisit == null) {
                m.setIsNew(true);
            } else {
                if (m.getCreationDate().compareTo(lastVisit) > 0) {
                    m.setIsNew(true);
                } else {
                    m.setIsNew(false);
                }
            }
        }
        return messages;
    }
}
